import allure
import pymysql
import pytest,requests,os
from dotenv import load_dotenv

load_dotenv()

@pytest.fixture(scope='function')
def login_token():
    url = 'http://10.59.9.18/small/user/v1/login'
    headers = {
        'Content-Type': 'application/json'
    }
    data1 = {
        'phone': os.getenv('LOGIN_PHONE'),
        'pwd': os.getenv('LOGIN_PWD'),
    }
    login = requests.post(url=url, headers=headers, json=data1)
    l_j=login.json()
    print(l_j)
    yield l_j['result']['sessionId'],l_j['result']['userId']
    # 执行数据库清除操作
    # db_deltable('李二二','17688889999')
    allure.attach.file(
        r'C:\Users\20176\Desktop\liu_twoweek\logs/rizhi.log',
        name='rizhi.log',
        attachment_type=allure.attachment_type.TEXT
    )


def db_deltable(real_name,phone):
    conn=pymysql.connect(
        host='10.59.9.19',
        user='yidongceshi',
        password='Yd123456!',
        db='mall'
    )
    cur=conn.cursor()
    query='delete from receive_address where real_name = %s and phone = %s'
    cur.execute(query,(real_name,phone))

    cur.close()
    conn.close()

